import 'dart:collection';

import 'package:dio/dio.dart';
import 'package:flutter_niandaxue/config/device.dart';
import 'dart:async';
import 'dart:io';

import '../config/index.dart';

Future request_post(url, {formData}) async{
  try{
    Response response;
    Dio dio = new Dio();
//    dio.options.contentType = ContentType.parse('application/x-www-form-urlencoded');
    dio.options.contentType = ContentType.parse('application/json');
    final String token = await getTokenLocal();
    dio.options.headers.addAll({KDevice.token_name: token});
    if(formData == null){
      response = await dio.post(servicePath[url]);
    }else{
      response = await dio.post(servicePath[url], data: formData);
    }
    if(response.statusCode == 200){
      return response;
    }else{
      throw Exception('请求服务数据错误：${response.statusCode}');;
    }
  }catch(e){
    return print('请求服务数据错：E');
  }
}

Future request_post_json(url, {formData}) async{
    Response response;
    Dio dio = new Dio();
    dio.options..contentType = ContentType.parse('application/json');
    final String token = await getTokenLocal();
    dio.options.headers.addAll({KDevice.token_name: token});
    if(formData == null){
      response = await dio.post(servicePath[url]);
    }else{
//      print(servicePath[url]);
//      print(formData);
      response = await dio.post(servicePath[url], data: formData);
    }
    if(response.statusCode == 200){
      return response;
    }
}

Future request_get_json(url, {formData}) async{
  Response response;
  Dio dio = new Dio();
  dio.options.contentType = ContentType.parse('application/json');
  final String token = await getTokenLocal();
  dio.options.headers.addAll({KDevice.token_name: token});
  if(formData == null){
    response = await dio.get(servicePath[url],);
  }else{
    response = await dio.get(servicePath[url] + formData );
  }
  if(response.statusCode == 200){
    return response;
  }
}

